package com.gzy.cbrsystem.shimian.action;



import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.search.*;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.mira.lucene.analysis.IK_CAnalyzer;

import com.gzy.cbrsystem.insomnia.InsomniaClinical;
import com.gzy.cbrsystem.insomnia.InsomniaClinicalDAO;

public class CreateIndex {


	/*ArrayList rtid = new ArrayList();*/
	
	protected InsomniaClinicalDAO insomniaClinicalDAO;
	protected InsomniaClinical InsomniaClinical;
	
	
	public InsomniaClinical getInsomniaClinical() {
		return InsomniaClinical;
	}
	public void setInsomniaClinical(InsomniaClinical insomniaClinical) {
		InsomniaClinical = insomniaClinical;
	}
	public InsomniaClinicalDAO getInsomniaClinicalDAO() {
		return insomniaClinicalDAO;
	}
	public void setInsomniaClinicalDAO(InsomniaClinicalDAO insomniaClinicalDAO) {
		this.insomniaClinicalDAO = insomniaClinicalDAO;
	}
	IK_CAnalyzer analyzer = new IK_CAnalyzer();
	//ChineseAnalyzer analyzer = new ChineseAnalyzer();
	String INDEX_PATH = "/CaseProject/WebRoot/WEB-INF";
	IndexReader reader;
	IndexWriter writer;
	
	public void  index(InsomniaClinical insomniaClinical) {
		String expr = null;
		try {
		        writer = new IndexWriter(INDEX_PATH, analyzer, true);
			    String id = insomniaClinical.getCaseId();
				String history = insomniaClinical.getHistory();
				String mainSympton=insomniaClinical.getMainSympton();
				String accompaniedSympton= insomniaClinical.getAccompaniedSympton();
				String tonguePluse=insomniaClinical.getTonguePluse();
				Document doc = new Document();
				Field field1 = new Field("ID", id, Field.Store.YES,
						Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
                doc.add(field1);
                expr=expr+history+mainSympton+accompaniedSympton+tonguePluse;
				Field field2 = new Field("EXPR", expr, Field.Store.YES,
						Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
                doc.add(field2);
				writer.addDocument(doc);	 	
			   }catch (Exception e) {
				   System.out.println(e.getMessage());
			   }finally{
				   try{
					   if (writer!=null){
						   	writer=null;
						   	writer.close();
						   }  
				   	}catch(Exception e){
				   		e.printStackTrace();
				   	}
				   
			   }
	}
	
	
	
	
}
	



